package library.and.xps.com.step.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;

import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.AbstractRecyclerAdapter;
import library.and.xps.com.step.adapter.PersonChallengeAdapter;
import library.and.xps.com.step.m.model.TeamChallengeRes;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.ListDividerItemDecoration;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.util.UserHelper;
import library.and.xps.com.step.view.base.BaseSwipeRefreshFragment;

//个人挑战
public class PersonChallengeFragment extends BaseSwipeRefreshFragment implements AbstractRecyclerAdapter.OnRecyclerViewItemClickListener {

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setBackgroundColor(Color.parseColor("#F0F0F0"));
        recyclerView.addItemDecoration(new ListDividerItemDecoration(0));
        abstractRecyclerAdapter = new PersonChallengeAdapter(activity, recyclerView);
        abstractRecyclerAdapter.setOnItemClickListener(this);
        setRefreshEnable(true);
        super.onActivityCreated(savedInstanceState);
        request(1);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        request(1);
    }

    public void request(int pageIndex) {
        String request = URLConstants.PERSONACTIVE.GETALLSELFPK + "?" + "teamid=" + TeamHelper.getIns().getCurT().getTeamid() + "&userid=" + UserHelper.getInstance().getUser().getUserid() + "&" + "page=" + pageIndex;
        OkHttpClientUtils.getDataAsync(getActivity(), request, new RequestCallback() {

            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
                onFail();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    Logger.json(response.getBody());
                    TeamChallengeRes team = new Gson().fromJson(response.getBody(), TeamChallengeRes.class);
                    adapterListData = team.getList();
                    onResponce();
                }
            }

            @Override
            public void onStart() {

            }
        }, "");
    }

    @Override
    protected void getData(int pageIndex) {

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
